//
//  CMDBModifyPwdViewController.swift
//  CMDB
//
//  Created by 王磊 on 18/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBModifyPwdViewController: NormalTitleViewController {
    
    lazy var originalPwd : CustomTF = {
        
        let originalPwd = CustomTF()
        
        originalPwd.iconImageName         = "密码"
        
        originalPwd.leftLabel.text        = "原密码:"
        
        originalPwd.leftLabel.textColor   = UIColor.Hex6(labelBlackColor, alpha:labelFirBlackAlpha)
        
        originalPwd.textColor             = UIColor.Hex6(labelBlackColor, alpha: labelSecBlacklAlpha)
        
        originalPwd.leftFont              = CMDB_16_FONT
        
        originalPwd.tid                   = "originalPwd"
        
        originalPwd.keyBoardType          = .NumberPad
        
        originalPwd.returnKeyType         = .Default
        
        originalPwd.maxLength             = Password_Max_Lenght
        
        originalPwd.attributedPlaceHolder = NSAttributedString(string: "初始密码", attributes: [NSFontAttributeName: CMDB_16_FONT,NSForegroundColorAttributeName: UIColor.Hex6(labelBlackColor, alpha: labelSecBlacklAlpha)])
        
        return originalPwd
        
    }()
    lazy var newPwd : CustomTF = {
        
        let newPwd = CustomTF()
        
        newPwd.iconImageName         = "密码"
        
        newPwd.leftLabel.text        = "新密码:"
        
        newPwd.leftLabel.textColor   = UIColor.Hex6(labelBlackColor, alpha:labelFirBlackAlpha)
        
        newPwd.textColor             = UIColor.Hex6(labelBlackColor, alpha: labelSecBlacklAlpha)
        
        newPwd.leftFont              = CMDB_16_FONT
        
        newPwd.tid                   = "newPwd"
        
        newPwd.keyBoardType          = .NumberPad
        
        newPwd.returnKeyType         = .Default
        
        newPwd.maxLength             = Password_Max_Lenght
        
        newPwd.attributedPlaceHolder = NSAttributedString(string: "8位以上数字与字母", attributes: [NSFontAttributeName: CMDB_16_FONT,NSForegroundColorAttributeName: UIColor.Hex6(labelBlackColor, alpha: labelSecBlacklAlpha)])
        
        return newPwd
        
    }()
    lazy var confirmPwd : CustomTF = {
        
        let confirmPwd = CustomTF()
        
        confirmPwd.iconImageName         = "密码"
        
        confirmPwd.leftLabel.text        = "确认密码:"
        
        confirmPwd.leftLabel.textColor   = UIColor.Hex6(labelBlackColor, alpha:labelFirBlackAlpha)
        
        confirmPwd.textColor             = UIColor.Hex6(labelBlackColor, alpha: labelSecBlacklAlpha)
        
        confirmPwd.leftFont              = CMDB_16_FONT
        
        confirmPwd.tid                   = "confirmPwd"
        
        confirmPwd.keyBoardType          = .NumberPad
        
        confirmPwd.returnKeyType         = .Default
        
        confirmPwd.maxLength             = Password_Max_Lenght
        
        confirmPwd.attributedPlaceHolder = NSAttributedString(string: "8位以上数字与字母", attributes: [NSFontAttributeName: CMDB_16_FONT,NSForegroundColorAttributeName: UIColor.Hex6(labelBlackColor, alpha: labelSecBlacklAlpha)])
        
        return confirmPwd
        
    }()
    private var confirmBtn: UIButton = UIButton(type: .Custom)
    
    override func setup() {
        super.setup()
        
        buildSubview()
    }
}
extension CMDBModifyPwdViewController {
    
    private func buildSubview() {
        
        contentView!.addSubview(originalPwd)
        
        contentView!.addSubview(newPwd)
        
        contentView!.addSubview(confirmPwd)
        
        contentView!.addSubview(confirmBtn)
        
        originalPwd.mDelegate = self
        
        let Left_Margin: CGFloat = (CMDB_Screen_Width - 630 * CMDB_Rate) * 0.5
        
        originalPwd.frame = CGRect(x: Left_Margin, y: 60 , width:630 * CMDB_Rate , height: 120 * CMDB_Rate)
        
        newPwd.frame = CGRect(x: Left_Margin, y: 120 * CMDB_Rate + 60, width:630 * CMDB_Rate , height: 120 * CMDB_Rate)
        
        confirmPwd.frame = CGRect(x: Left_Margin, y: 120 * CMDB_Rate * 2 + 60, width:630 * CMDB_Rate , height: 120 * CMDB_Rate)
        
        confirmBtn.setTitle("注册", forState: .Normal)
        
        confirmBtn.setTitle("注册", forState: .Highlighted)
        
        confirmBtn.setTitleColor(UIColor.Hex6(0xffffff, alpha: 0.54), forState: .Highlighted)
        
        confirmBtn.setTitleColor(UIColor.Hex6(0xffffff), forState: .Normal)
        
        confirmBtn.backgroundColor = UIColor.Hex6(0xff6d00, alpha: 54)
        
        confirmBtn.titleLabel!.font = CMDB_17_FONT
        
        confirmBtn.addTarget(self, action: #selector(confirmBtnTriggrred), forControlEvents: .TouchUpInside)
        
        confirmBtn.layer.cornerRadius = 5
        
        confirmBtn.frame = CGRectMake(Left_Margin, confirmPwd.y + 120 * CMDB_Rate + 120 * CMDB_Rate, newPwd.width, 88 * CMDB_Rate)
    }
}
extension CMDBModifyPwdViewController {
    
    @objc private func confirmBtnTriggrred() {
        
        let modifyPwdParam = CMDBModifyPwdParam()
        
        modifyPwdParam.password = originalPwd.text ?? ""
        
        modifyPwdParam.newPassword = newPwd.text ?? ""
        
        modifyPwdParam.confirmPassword = confirmPwd.text ?? ""
        
        do {
            
            try CMDBPasswordManager.getInstance.modifyPwd(modifyPwdParam, handler: { (isSuccess) in
                
                
                }, error: { (rescode) in
                    
                    
                }, failure: {
                    
            })
        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid) {
            
            CMDBShowInfoManager.getInstance.showInfoWithStatus("原密码长度最少为8位数字与字母组合!")
            
        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.NewPasswordInvalid) {
            
            CMDBShowInfoManager.getInstance.showInfoWithStatus("新密码长度最少为8位数字与字母组合!")
            
        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.ConfirmPasswordInvalid) {
            
            CMDBShowInfoManager.getInstance.showInfoWithStatus("确认密码长度最少为8位数字与字母组合!")
            
        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.OldPwdNotMatchNewPwd) {
            
            CMDBShowInfoManager.getInstance.showInfoWithStatus("新密码与确认密码不匹配!")
            
        } catch {
            
            printLog(error)
        }
        
    }
    
}
